<?php
namespace App\Model;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use App\Model\{Admin,Config}; 

class Wxpay extends Model
{
	public function unifiedorder($p){


        $data['spbill_create_ip'] = $p['ip'];
        $data['nonce_str'] = rand(10000,999999);
        $data['nonce_str'] = md5(rand(1,9999));
        $data['sign_type'] = 'MD5';

        $data['body'] = $p['title']??'创鲲公仔';

        $data['out_trade_no'] = Func::order_no();
        $data['total_fee'] = $p['total_fee']??'102';
        #$data['time_expire'] = date('YmdHis',strtotime("+3 hour"));
        $data['notify_url']=config('wxpay')['notify_url'];
        $data['trade_type'] = 'APP';
        $data = $this->basedata($data);
        ksort($data);
        $sign = $this->getsign($data);

        $data['sign'] = $sign;
        #var_dump($data);exit;
        $post = $this->arr2xml($data);

        $rs = $this->curl('pay/unifiedorder',$post);
        return $rs;

    }


    /**
     * 微信订单查询接口
     * $p['partner_trade_no']
     */
    public function get_info($p){
        $data['nonce_str'] = rand(10000,999999);
        $data['nonce_str'] = md5(rand(1,9999));
        $data['partner_trade_no'] = $p['partner_trade_no'];
        $data = $this->basedata($data);

        $sign = $this->getsign($data);
        $data['sign'] = $sign;

        $post = $this->arr2xml($data);
        $rs = $this->curl('mmpaymkttransfers/gettransferinfo',$post);
        return $rs;
    }

    public function basedata($p=[]){
        $data = [
            'appid'=>config('wxpay')['appid'],
            'mch_id'=>config('wxpay')['mch_id'],
        ];
        if(!empty($p)){
            $data = array_merge($p,$data);
        }
        return $data;
    }

    public function getsign($data,$bool=false){
        ksort($data);
        $str="";
        foreach($data as $key=>$val){
            if(!empty($val)){
                $str.=$key.'='.$val.'&';
            }
        }
        $rs = $str;
        if(config('wxpay')['test']==true && $bool==true){
            $key = $this->sandbox_getsign($data);
        }else{
            $key = config('wxpay')['key'];
        }

        $str .= "key=".$key;
        $temp = md5($str);
        $temp=strtoupper($temp);
        return $temp;
    }

    public function sandbox_getsign($data){
        ksort($data);
        $str="";
        foreach($data as $key=>$val){
            if(!empty($val)){
                $str.=$key.'='.$val.'&';
            }
        }
        $rs = $str;
        $key = config('wxpay')['key'];
        $str .= "key=".$key;
        $temp = md5($str);
        $temp=strtoupper($temp);

        $data['sign'] = $temp;
        $str = $this->arr2xml($data);
        $data = $this->curl("https://api.mch.weixin.qq.com/sandboxnew/pay/getsignkey",$str);
        $key = $data['sandbox_signkey'];
        return $key;
    }

    public function getlinesign($data,$bool=false){
        ksort($data);
        $str="";
        foreach($data as $key=>$val){
            if(!empty($val)){
                $str.=$key.'='.$val.'&';
            }
        }
        $rs = $str;
        $key = config('wxpay')['key'];
        
        $str .= "key=".$key;
        $temp = md5($str);
        $temp=strtoupper($temp);
        return $temp;
    }

    public function curl($url,$data){
        $url = config('wxpay')['api'].$url;
        //设置超时
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_TIMEOUT, 300);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt( $ch, CURLOPT_POST, 1 );
        curl_setopt( $ch, CURLOPT_POSTFIELDS, $data );
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,FALSE);
        curl_setopt($ch, CURLOPT_HEADER, FALSE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

		curl_setopt($ch, CURLOPT_SSLCERT,'/data/www/songshu.com/config/wxpay/apiclient_cert.pem'); //client.    
		curl_setopt($ch, CURLOPT_SSLKEY,'/data/www/songshu.com/config/wxpay/apiclient_key.pem');  
        $res = curl_exec($ch);
        curl_close($ch);
        $data = $this->xml2arr($res);
        return $data;
    }



    function arr2xml($data, $root = true){
        $xml = "<xml>\n";
        foreach($data as $key=>$val){
            $xml .="<$key>$val</$key>\n";
        }
        $xml = $xml."</xml>";
        return $xml;
    }
    public function xml2arr($xml){
        $xml= simplexml_load_string($xml,'SimpleXMLElement', LIBXML_NOCDATA);
        $xmljson = json_encode($xml);
        $xmlarray=json_decode($xmljson,true);//将json转换
        return $xmlarray;
    }


    /**
     * 提现
     * $p['partner_trade_no'] 商户订单号
     * $p['openid'] openid
     * $p['amount']
     * $p['ip']
     * $p['re_user_name']
     * $p['spbill_create_ip']
     */
    public function cash($p){
        $data = [
            'partner_trade_no'=>$p['partner_trade_no'],
            'openid'=>$p['openid'],
            'check_name'=>'FORCE_CHECK',
            'amount'=>$p['amount']*100,
            'desc'=>'合伙人提现',
            'spbill_create_ip'=>$p['spbill_create_ip'],
            're_user_name'=>$p['re_user_name'],
            'mch_appid'=>config('wxpay')['appid'],
            'mchid'=>config('wxpay')['mch_id'],
            'desc'=>$p['desc'],
        ];
        #dd($data);
        $data['nonce_str'] = md5(rand(1,9999));
   
        $sign = $this->getlinesign($data);
        $data['sign'] = $sign;
        $post = $this->arr2xml($data);
        $rs = $this->curl('mmpaymkttransfers/promotion/transfers',$post);
        return $rs;
    }
}
